// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: GHZ Setup Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//

private alias object.value0 : object.timer
private alias object.value1 : object.index

private alias object.value2 : object.drawX
private alias object.value3 : object.drawY
private alias object.value4 : object.letter
private alias object.value5 : object.charamount
private alias object.value6 : object.boxYpos
private alias object.value7 : object.letterlag
private alias object.value8 : object.callend

public value BoxPositionY = 0;
public value CharacterTalking = 0;
public value TextActive = 0;
public value CutsceneActive = 0;
public value CallEnd = 0;

public alias saveRAM[286] : saved.language

reserve function TextDrawing

function TextDrawing
	temp0 = object.drawX
	temp1 = object.charamount
	while temp1 > 0
	// Get the actual character at the current char pos
	GetTextInfo(object.letter, 1, TEXTINFO_TEXTDATA, Object.Index, ArrayPos0)

	Object.Frame = 0
	
	// Take uppercase letters and turn them into a matching Sprite Frame
	if object.letter > 64
		if object.letter < 91
			Object.Frame  = object.letter
			Object.Frame -= 64
		end if
	end if
	
	// Take lowercase letters and turn them into a matching Sprite Frame
	if object.letter > 96
		if object.letter < 123
			Object.Frame  = object.letter
			Object.Frame -= 96
		end if
	end if

		//ASCII Table required!
		switch object.letter
			case 33	//!
				DrawSpriteScreenFX(37, FX_INK, object.drawX, object.drawY)
				ArrayPos0++
				temp1--
				object.drawX += 0x8
				break		
			case 63	//?
				DrawSpriteScreenFX(38, FX_INK, object.drawX, object.drawY)
				ArrayPos0++
				temp1--
				object.drawX += 0x8
				break			
			case 39	//''
				DrawSpriteScreenFX(42, FX_INK, object.drawX, object.drawY)
				ArrayPos0++
				temp1--
				object.drawX += 0x8
				break						
			case 42	//*
				DrawSpriteScreenFX(34, FX_INK, object.drawX, object.drawY)
				ArrayPos0++
				temp1--
				object.drawX += 0x8
				break
			case 44	//,
				DrawSpriteScreenFX(40, FX_INK, object.drawX, object.drawY)
				ArrayPos0++
				temp1--
				object.drawX += 0x8
				break		
			case 46	//.
				DrawSpriteScreenFX(41, FX_INK, object.drawX, object.drawY)
				ArrayPos0++
				temp1--
				object.drawX += 0x8
				break			
			case 58	//:
				DrawSpriteScreenFX(43, FX_INK, object.drawX, object.drawY)
				ArrayPos0++
				temp1--
				object.drawX += 0x8
				break		
			case 51	// 3
				DrawSpriteScreenFX(47, FX_INK, object.drawX, object.drawY)
				ArrayPos0++
				temp1--
				object.drawX += 0x8
				break		
			case 52// 4
				DrawSpriteScreenFX(48, FX_INK, object.drawX, object.drawY)
				ArrayPos0++
				temp1--
				object.drawX += 0x8
				break							
			case 57	// 9
				DrawSpriteScreenFX(53, FX_INK, object.drawX, object.drawY)
				ArrayPos0++
				temp1--
				object.drawX += 0x8
				break	
			case 64	//@	LINE SKIPPER
				object.drawY += 16
				if CharacterTalking == 0
					temp0 -= 8
					object.drawX = temp0
				else
					temp0 += 8
					object.drawX = temp0
				end if

				// Move to the next letter
				ArrayPos0++
				
				// And decrease the remaining letter count, as well
				temp1--
			break
			default
			// Notably, the space character (32) is skipped over here, as it shouldn't draw anything
			

			DrawSpriteScreenFX(Object.Frame, FX_INK, object.drawX, object.drawY)

			// Move to the next letter
			ArrayPos0++
			
			// And decrease the remaining letter count, as well
			temp1--
			
			// Each letter should be spaced 8 pixels apart
			object.drawX += 0x8
			break
		end switch
	loop
end function

//if being stupid was a job

event ObjectMain
	if stage.actnum > 5
		object.type = TypeName[Blank Object]
	end if	
	switch object.state
	case 0	//activation
		if object[5].type == TypeName[Blank Object]
			object.timer++
			if object.timer == 30
				PlaySfx(SfxName[Phone], false)
			end if	

			if object.timer == 350
				object.alpha = 0
				switch stage.actnum
				case 1
					Object.Index = 15
				break
				case 2
					Object.Index = 39
				break			
				case 3
					Object.Index = 54
				break					
				case 4
					Object.Index = 71
				break			
				case 5
					Object.Index = 84
				break							
				end switch
				switch stage.actnum
				case 1
					object.callend = 37
				break
				case 2
					object.callend = 53
				break
				case 3
					object.callend = 63
				break				
				case 4
					object.callend = 82
				break		
				case 5
					object.callend = 86
				break									
				end switch				
				object.letterlag = 0
				TextActive = 1
				BoxPositionY = 0
				object.timer = 0
				object.state++
			end if

		end if	
	break	
	case 1	
		object.alpha += 8
		object.timer++
		object.charamount = 10
		if object.timer == 30
			object.alpha = 255
			object.timer = 0
			object.state++
		end if
	break
	case 2
	


		if object.letterlag > 0 
			object.letterlag--
		end if

		if object.letterlag == 0	

			GetTextInfo(temp0, 1, TEXTINFO_TEXTSIZE, Object.Index, 0)
			if object.charamount == temp0
				object.timer++
				if object.timer == 200
					if Object.Index >= object.callend
						object.type = TypeName[Blank Object]
					else
						Object.Index++					
						object.charamount = 10
						object.timer = 0
					end if
				end if
			else
				object.charamount++
				PlaySfx(SfxName[Score Add], false)				
				object.timer = 0

				if object.letter == 43
					object.letterlag = 100
				end if

			end if

		end if
	
	break
	end switch
end event

event ObjectDraw
	object.inkEffect = INK_ALPHA

	ArrayPos0 = 0	//starting letter



	if TextActive == 1

		
		object.drawY = 42

		temp3 = screen.xcenter
		temp3 -= 150
		object.drawX = temp3
		

		CallFunction(TextDrawing)
	end if	

	if object.state >= 13
		DrawRect(0, 0, screen.xsize, screen.ysize, color7, color7, color7, 255)

		temp1 = screen.xcenter

		DrawSpriteScreenFX(39, FX_INK, temp1, screen.ycenter)

	end if



end event

event ObjectStartup

	LoadSpriteSheet("FNAF/Objects1.gif")

	switch saved.language
		case 0
			LoadTextFile(1, "Data/Game/Tutorial.txt")
		break
		case 1
			LoadTextFile(1, "Data/Game/Portuguese/Tutorial.txt")
		break
		case 2
			LoadTextFile(1, "Data/Game/TutorialSP.txt")
		break
		case 3
			LoadTextFile(1, "Data/Game/French/TutorialFR.txt")
		break		
	end switch

	BoxPositionY = 0
	CharacterTalking = 0
	TextActive = 0
	CutsceneActive = 0	
	CallEnd = 0

	SpriteFrame(0, 0, 0, 0, 0, 0)	//0 the bar

	// #1 -> #26 - Alphabet
	// (Using loops to automate Sprite Framing)

	//not related too stuff in objectdraw

	//temp7: starting ypos, progresses by 17 and stops before 100, because theres only 3 rows
	//temp6: starting xpos, the letter A
	temp1 = 49
	while temp1 < 100
		temp0 = 549
		while temp0 < 648
			SpriteFrame(-3, -6, 8, 7, temp0, temp1)
			temp0 += 9
		loop
		temp1 += 17
	loop

	TextActive = 0

	//undertale
	SpriteFrame(-3, -6, 8, 16, 513, 100) //27

	//undertale
	SpriteFrame(-180, -14, 64, 64, 578, 117) //35 slender man icon

	SpriteFrame(-160, -22, 320, 80, 26, 683) //36 slender man icon	
	
	//undertale
	SpriteFrame(-3, -6, 8, 16, 522, 100) //37
	
	
	//undertale
	SpriteFrame(-3, -6, 8, 16, 531, 100) //38

	//undertale
	SpriteFrame(-130, -128, 256, 256, 2, 766) //39


	SpriteFrame(-3, -6, 8, 16, 540, 83) //40	,


	SpriteFrame(-3, -6, 8, 16, 540, 100) //41	.
	SpriteFrame(-3, -6, 8, 16, 549, 100) //42	`

	SpriteFrame(-3, -6, 8, 16, 558, 100) //43	`

	//#44 - idk numbers
	temp1 = 49
	while temp1 < 100
		temp0 = 513
		while temp0 < 549
			SpriteFrame(-3, -6, 8, 16, temp0, temp1)
			temp0 += 9
		loop
		temp1 += 17
	loop


	// Place the HUD object into reserved object slot 4 and initialise its values
	Object[30].Type = TypeName[Phone Guy]
	
	// The object should always be run, regardless if it's within camera bounds or not
	Object[30].Priority = PRIORITY_ACTIVE
	
	// Make the HUD draw ontop of all other sprites, too
	Object[30].DrawOrder = 6

end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("IIZ/Objects1.gif")
	SpriteFrame(0, 0, 64, 64, 153, 154)
end event
